package com.adm.ctl;

import java.io.IOException;
import java.util.Map;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.yfsoft.comm.util.BizResult;
import org.yfsoft.comm.util.ServletUtil;
import org.yfsoft.comm.util.StringUtil;

import com.adm.biz.Service;

public class CoreCtl extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private ApplicationContext ctx = null;
	@Override
	public void init(ServletConfig config) throws ServletException {
		super.init(config);
		//加载spring配置文件
		this.ctx = new ClassPathXmlApplicationContext("beans.xml");
	}

	
	protected void doGet(HttpServletRequest request, HttpServletResponse response) 
			throws ServletException, IOException {
		request.setCharacterEncoding("utf8");
		response.setContentType("text/html;charset=UTF-8");
		//解析url参数
		Map<String,String> params = ServletUtil.getParamters(request);
		//获取到service
		Service s = (Service)this.ctx.getBean(params.get("s"));
		params.remove("s");
		try {
			s.execute(params,request,response);
		} catch (Throwable e) {
			e.printStackTrace();
			throw new ServletException(e.getMessage());
		}
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request,response);
	}

}
